package com.appspot.zarm.gomoku;

import com.appspot.zarm.game.IUser;
import com.appspot.zarm.game.Player;
import com.appspot.zarm.game.PlayerType;

public class GomokuGameFacade {

	private GomokuGame game;
	private Player player;
	private Player rival;
	private GomokuBoard board;

	public GomokuGameFacade(GomokuGame game, IUser user) {
		this.game = game;
		player = game.getPlayer(user);
		rival = game.getRival(player);
		board = (GomokuBoard) this.game.getBoard();
	}

	public void doTurn(Coord position) {
		Cell cell = board.getCell(position);
		// player`s turn
		if (game.isThisPlayerTurn(player)) {
			if (cell.isEmpty()) {
				if (player.getType() == PlayerType.ONE) {
					cell.setCellState(CellState.CROSS);
				} else {
					cell.setCellState(CellState.ZERO);
				}
				game.setThisPlayerTurn(rival);
			}
		}
		
		// rival`s turn
		if (rival != null && rival.isRobot() && game.isThisPlayerTurn(rival)) {

			position = game.getRobot().doTurn(board);

			if (position != null) {
				cell = board.getCell(position);

				if (cell.isEmpty()) {
					if (rival.getType() == PlayerType.ONE) {
						cell.setCellState(CellState.CROSS);
					} else {
						cell.setCellState(CellState.ZERO);
					}

					game.setThisPlayerTurn(player);
				}

			}

		}
	}

	public void finish() {
	}

	public void start() {
	}

}
